<?php

require_once 'Interfaces/AIS_JsonPrcMethodParamsValidator.php';

class AIS_JsonRpcMethodParamsValidatorImpl implements AIS_JsonPrcMethodParamsValidator
{
	
	function __construct() {
		$this->compatibilityValidator = new AIS_ObjectTypeCompatibilityValidator();
	}
	/**
	 * Function checks that the parameters sent by Json are in the form the Function called expects them.
	 * This may be quite slow for large non-primitive-Parameters. If a type of a parameter is a class
	 * that contains cyclic references, the function will always return false.
	 *
	 * @param ReflectionMethod $method The method we want to test the parameters against
	 * @param array $params This are the parameters sent over Json and decoded to an array
	 * @return bool True if the params in array have expected structure, false otherwise.
	 */
	public function validate(ReflectionMethod $method, $params)
	{
		$i = 0;
		$b = true;
		if (!is_array($params))
		{
			$this->errorMessage = new AIS_ObjectTypeCompatibilityValidatorError();
			$this->errorMessage->message = "Parameters must be an Array";
			$this->errorMessage->checkedType = "array";
			$this->errorMessage->receivedValue = $params;
			return false;
		}
		$doc = new AIS_DocCommentFunction($method->getDocComment());
		$paramTypes = $doc->getTags("@param");
		foreach($params as $param)
		{
			$tag = $paramTypes[$i];
			$b = $b && $this->compatibilityValidator->validateObjectWithDocCommentTag($param, $tag);
			if (!$b)
			{
				$this->errorMessage = $this->compatibilityValidator->getErrorMessage();
				$this->errorMessage->trace = "param[".$i."]".$this->errorMessage->trace;
				return false;
			}
			$i++;
		}
		return $b;
	}
	/**
	 * Returns the Error Object when something went wrong
	 *
	 * @return AIS_ObjectTypeCompatibilityValidatorError
	 */
	public function getErrorMessage()
	{
		return $this->errorMessage;
	}
	/**
	 * Enter description here...
	 *
	 * @var AIS_ObjectTypeCompatibilityValidator
	 */
	private $compatibilityValidator;
	/**
	 * Enter description here...
	 *
	 * @var AIS_ObjectTypeCompatibilityValidatorError
	 */
	private $errorMessage;
}
?>